package com.dc.free.controller.charity;

import java.util.Date;

import com.dc.free.controller.BaseController;
import com.dc.free.db.model.CAction;
import com.dc.free.db.model.CUser;
import com.dc.free.db.service.CAtionService;
import com.dc.free.db.service.CBiddingService;

public class BiddingController extends BaseController {

	public void list() {
		int pageNumber = getParaToInt("page_number", 1);
		int pageSize = getParaToInt("page_size", 20);
		String actionId = data.getString("actionId");
		String goodsId = getPara("goods_id");
		
		renderStatus(CBiddingService.list(actionId, goodsId, pageNumber, pageSize));
	}
	
	/**
	 * 竞价
	 */
	public void save() {
		CUser user = getUser();
		String goodsId = getPara("goods_id");
		int price = getParaToInt("price");
		String actionId = data.getString("actionId");
		CAction action = CAtionService.getAction(actionId);
		
		//活动未开始
		if(new Date().before(action.getStartTime())) {
			renderStatus(100003);
			return;
		}
		
		//活动已结束
		if(new Date().after(action.getEndTime())) {
			renderStatus(100004);
			return;
		}
		
		boolean isSuccess = CBiddingService.insert(goodsId, user.getId(), price);
		
		if(isSuccess)
			renderStatus();
		else
			renderStatus(100002);
	}
}
